const config = require('./config')
const themeListeners = []
global.isDemo = true
import api from './util/util';
App({

	onLaunch(opts, data) {

		// if (canIUseSetBackgroundFetchToken) {
		//   wx.setBackgroundFetchToken({
		//     token: 'getBackgroundFetchToken',
		//   })
		// }
		// if (wx.getBackgroundFetchData) {
		//   wx.getBackgroundFetchData({
		//     fetchType: 'pre',
		//     success(res) {
		//       that.globalData.backgroundFetchData  = res;
		//       console.log('读取预拉取数据成功')
		//     },
		//     fail() {
		//       console.log('读取预拉取数据失败')
		//       wx.showToast({
		//         title: '无缓存数据',
		//         icon: 'none'
		//       })
		//     },
		//     complete() {
		//       console.log('结束读取')
		//     }
		//   })
		// }
		console.log('App Launch', opts)
		if (data && data.path) {
			wx.navigateTo({
				url: data.path,
			})
		}
		if (!wx.cloud) {
			console.error('请使用 2.2.3 或以上的基础库以使用云能力')
		} else {
			wx.cloud.init({
				env: config.envId,
				traceUser: true,
			})
		}

		// 获取系统的信息
		const systemData = wx.getWindowInfo();
		console.log('systemData', systemData)
		const menuInfo = wx.getMenuButtonBoundingClientRect();
		console.log('menuInfo', menuInfo);
		this.globalData.systemInfoData = systemData;
		this.globalData.menuInfo = menuInfo;
	},

	onShow(opts) {
		console.log('App Show', opts)
		// console.log(wx.getSystemInfoSync())
	},
	onHide() {
		console.log('App Hide')
	},
	onThemeChange({
		theme
	}) {
		this.globalData.theme = theme
		themeListeners.forEach((listener) => {
			listener(theme)
		})
	},
	watchThemeChange(listener) {
		if (themeListeners.indexOf(listener) < 0) {
			themeListeners.push(listener)
		}
	},
	unWatchThemeChange(listener) {
		const index = themeListeners.indexOf(listener)
		if (index > -1) {
			themeListeners.splice(index, 1)
		}
	},
	globalData: {
		theme: wx.getSystemInfoSync().theme,
		hasLogin: false,
		openid: null,
		systemInfoData: null,
		menuInfo: null,
		iconTabbar: '/page/weui/example/images/icon_tabbar.png',
		videoUrl: 'https://product.lvcampaign.com/attached/107/videos/2025-08-20/9d2ed0c1d3954ec5a6c3cca43009a78b.mp4'
	},
	api: api,
	// lazy loading openid
	getUserOpenId(callback) {
		const self = this

		if (self.globalData.openid) {
			callback(null, self.globalData.openid)
		} else {
			wx.login({
				success(data) {
					wx.cloud.callFunction({
						name: 'login',
						data: {
							action: 'openid'
						},
						success: res => {
							console.log('拉取openid成功', res)
							self.globalData.openid = res.result.openid
							callback(null, self.globalData.openid)
						},
						fail: err => {
							console.log('拉取用户openid失败，将无法正常使用开放接口等服务', res)
							callback(res)
						}
					})
				},
				fail(err) {
					console.log('wx.login 接口调用失败，将无法正常使用开放接口等服务', err)
					callback(err)
				}
			})
		}
	},
	// 通过云函数获取用户 openid，支持回调或 Promise
	getUserOpenIdViaCloud() {
		return wx.cloud.callFunction({
			name: 'wxContext',
			data: {}
		}).then(res => {
			this.globalData.openid = res.result.openid
			return res.result.openid
		})
	}

})